package cn.iocoder.yudao.module.resource.controller.admin.creator;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.resource.controller.admin.creator.vo.ResourceCreatorPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.creator.vo.ResourceCreatorRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.creator.vo.ResourceCreatorSaveReqVO;
import cn.iocoder.yudao.module.resource.convert.ResourceCreatorConvert;
import cn.iocoder.yudao.module.resource.dal.dataobject.creator.ResourceCreatorDO;
import cn.iocoder.yudao.module.resource.service.creator.ResourceCreatorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 创作者")
@RestController
@RequestMapping("/resource/creator")
@Validated
public class ResourceCreatorController {

    @Resource
    private ResourceCreatorService creatorService;

    @PostMapping("/create")
    @Operation(summary = "创建创作者")
    @PreAuthorize("@ss.hasPermission('resource:creator:create')")
    public CommonResult<Long> createCreator(@Valid @RequestBody ResourceCreatorSaveReqVO createReqVO) {
        return success(creatorService.createCreator(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新创作者")
    @PreAuthorize("@ss.hasPermission('resource:creator:update')")
    public CommonResult<Boolean> updateCreator(@Valid @RequestBody ResourceCreatorSaveReqVO updateReqVO) {
        creatorService.updateCreator(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除创作者")
    @Parameter(name = "id", description = "创作者ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:creator:delete')")
    public CommonResult<Boolean> deleteCreator(@RequestParam("id") Long id) {
        creatorService.deleteCreator(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Operation(summary = "批量删除创作者")
    @Parameter(name = "ids", description = "创作者ID列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('resource:creator:delete')")
    public CommonResult<Boolean> deleteCreatorList(@RequestParam("ids") List<Long> ids) {
        creatorService.deleteCreatorList(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得创作者")
    @Parameter(name = "id", description = "创作者ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:creator:query')")
    public CommonResult<ResourceCreatorRespVO> getCreator(@RequestParam("id") Long id) {
        ResourceCreatorDO creator = creatorService.getCreator(id);
        return success(BeanUtils.toBean(creator, ResourceCreatorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得创作者分页")
    @PreAuthorize("@ss.hasPermission('resource:creator:query')")
    public CommonResult<PageResult<ResourceCreatorRespVO>> getCreatorPage(@Valid ResourceCreatorPageReqVO pageReqVO) {
        PageResult<ResourceCreatorDO> pageResult = creatorService.getCreatorPage(pageReqVO);
        return success(ResourceCreatorConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出创作者 Excel")
    @PreAuthorize("@ss.hasPermission('resource:creator:export')")
    public void exportCreatorExcel(@Valid ResourceCreatorPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceCreatorDO> list = creatorService.getCreatorPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "创作者.xls", "数据", ResourceCreatorRespVO.class,
                BeanUtils.toBean(list, ResourceCreatorRespVO.class));
    }

}
